package zkk.com.mengqu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import zkk.com.mengqu.fragment.HandbookFragment;
import zkk.com.mengqu.fragment.HomeFragment;
import zkk.com.mengqu.fragment.StewardFragment;
import zkk.com.mengqu.fragment.UserFragment;

/**
 * 主页面
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private FragmentManager fm;
    private FragmentTransaction ft;
    private LinearLayout ll_home,ll_handbook,ll_circle_friend,ll_user;

    private long mExitTime;
    private Fragment fromFrament;
    private HomeFragment firstFragment;//首页
//    private HandbookFragment handbookFragment;//指南
    private StewardFragment secondFragment;//饭友圈
    private UserFragment thirdFragment;//我的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    private void initView(){
        ll_home = (LinearLayout)findViewById(R.id.ll_home);
//        ll_handbook = (LinearLayout)findViewById(R.id.ll_handbook);
        ll_circle_friend = (LinearLayout)findViewById(R.id.ll_circle_friend);
        ll_user = (LinearLayout)findViewById(R.id.ll_user);
        fm = getSupportFragmentManager();

        ll_home.setOnClickListener(this);
//        ll_handbook.setOnClickListener(this);
        ll_circle_friend.setOnClickListener(this);
        ll_user.setOnClickListener(this);

        change_main_picture();
    }

    //点击底部导航栏
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_home:
                changeHome();
                ll_home.setSelected(true);
//                ll_handbook.setSelected(false);
                ll_circle_friend.setSelected(false);
                ll_user.setSelected(false);
                break;
//            case R.id.ll_handbook:
//                changeHandbook();
//                ll_home.setSelected(false);
//                ll_handbook.setSelected(true);
//                ll_circle_friend.setSelected(false);
//                ll_user.setSelected(false);
//                break;
            case R.id.ll_circle_friend:
                changeCirecleFriend();
                ll_home.setSelected(false);
//                ll_handbook.setSelected(false);
                ll_circle_friend.setSelected(true);
                ll_user.setSelected(false);
                break;
            case R.id.ll_user:
                changeMy();
                ll_home.setSelected(false);
//                ll_handbook.setSelected(false);
                ll_circle_friend.setSelected(false);
                ll_user.setSelected(true);
                break;
        }
    }

    //初始状态
    private void change_main_picture(){
        ll_home.setSelected(true);
        ft = fm.beginTransaction();
        if(firstFragment == null){
            firstFragment = new HomeFragment();
            fromFrament = firstFragment;
        }
        ft.replace(R.id.frameContent, firstFragment);
        ft.commit();
    }

    //首页
    private void changeHome(){
        ll_home.setSelected(true);
        ft = fm.beginTransaction();
        if(firstFragment == null){
            firstFragment = new HomeFragment();
        }
        if (!firstFragment.isAdded()) {    // 先判断是否被add过
            ft.hide(fromFrament).add(R.id.frameContent, firstFragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
        } else {
            ft.hide(fromFrament).show(firstFragment).commit(); // 隐藏当前的fragment，显示下一个
        }
        fromFrament = firstFragment;
    }

    //指南
//    private void changeHandbook(){
//        ll_handbook.setSelected(true);
//        ft = fm.beginTransaction();
//        if(handbookFragment==null){
//            handbookFragment=new HandbookFragment();
//        }
//        if (!handbookFragment.isAdded()) {    // 先判断是否被add过
//            ft.hide(fromFrament).add(R.id.frameContent, handbookFragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
//        } else {
//            ft.hide(fromFrament).show(handbookFragment).commit(); // 隐藏当前的fragment，显示下一个
//        }
//        fromFrament = handbookFragment;
//    }

    //饭友圈
    private void changeCirecleFriend(){
        ll_circle_friend.setSelected(true);
        ft = fm.beginTransaction();
        if(secondFragment == null){
            secondFragment = new StewardFragment();
        }
        if (!secondFragment.isAdded()) {    // 先判断是否被add过
            ft.hide(fromFrament).add(R.id.frameContent, secondFragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
        } else {
            ft.hide(fromFrament).show(secondFragment).commit(); // 隐藏当前的fragment，显示下一个
        }
        fromFrament = secondFragment;
    }

    //我的
    private void changeMy(){
        ll_user.setSelected(true);
        ft = fm.beginTransaction();
        if(thirdFragment == null){
            thirdFragment = new UserFragment();
        }
        if (!thirdFragment.isAdded()) {    // 先判断是否被add过
            ft.hide(fromFrament).add(R.id.frameContent, thirdFragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
        } else {
            ft.hide(fromFrament).show(thirdFragment).commit(); // 隐藏当前的fragment，显示下一个
        }
        fromFrament=thirdFragment;
    }

    //双击退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
			/*
			 * if(mMapView!=null){ mMapView.destroy(); }
			 */
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            }
            return true;
        }
        // 拦截MENU按钮点击事件，让他无任何操作
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
